package com.zmcn.flowindex.fw.tool.module;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zmcn.flowindex.fw.tool.bean.ColVO;
import com.zmcn.flowindex.fw.tool.bean.DbScriptMysqlParse;
import com.zmcn.flowindex.fw.tool.bean.TableVO;
import com.zmcn.flowindex.fw.util.FileOP;

public class ModuleOut {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String modulePath = ModuleOut.class.getResource("/mymodule").getFile();
		String outJavaPath = "N:/SL/trunk/src/server2/src/main/java/com.zmcn.flowindex/sl/business";
		String outJspPath = "N:/SL/trunk/src/server2/src/main/webapp/WEB-INF/jsp";
		Map<String, String> map = getPara();
		doFile(new File(modulePath), map, outJavaPath + "/" + map.get("moduleL"), outJspPath + "/" + map.get("moduleL"));
	}

	private static Map<String, String> getPara() {
		Map<String, String> map = new HashMap<String, String>();

		String title = "";
		String module = "adb";
		String pack = "com.zmcn.flowindex.sl";
		String voName = "";
		// -------------------------
		List<TableVO> list = DbScriptMysqlParse.parse("N:/SL/trunk/script/table.sql", "gb2312");
		List<ColVO> cols = new ArrayList<ColVO>();// 字段列表
		for (TableVO vo : list) {
			if (vo.getTable().getName().equals("T_ADB_DRIVER")) {
				cols = vo.getCols();
				String name = vo.getTable().getName();
				if (name.startsWith("T_")) {
					name = name.substring(2);
				}
				voName = getFBE(name);
				title = vo.getTable().getDesc();
				break;
			}
		}
		// --------------------------
		map.put("title", title);
		map.put("module", module);
		map.put("moduleL", getFL(module));
		map.put("pack", pack);
		map.put("voName", voName);
		map.put("voNameL", getFL(voName));
		map.put("voNameS", voName.toLowerCase());
		map.put("reqGet", getReqGet(cols));
		map.put("paras", getParas(cols));
		map.put("typeParas", getTypeParas(cols));
		map.put("descs", getDescs(cols));
		map.put("id", getIdName(cols));
		map.put("setParas", getSetParas(cols, map.get("voNameL")));
		return map;
	}

	/**
	 * 替换文件内容和输出文件
	 * 
	 * @param file
	 * @param map
	 * @param outPath
	 */
	private static void doFile(File file, Map<String, String> map, String outJavaPath, String outJspPath) {
		for (File f : file.listFiles()) {
			if (f.isDirectory()) {
				if (f.getName().endsWith("svn")) {
					continue;
				}
				f.mkdirs();
				doFile(f, map, outJavaPath + "/" + f.getName(), outJspPath + "/" + f.getName());
			} else {
				String s = FileOP.readFile(f.getPath(), "utf-8");
				for (String key : map.keySet()) {
					s = s.replaceAll("\\{" + key + "\\}", map.get(key));
				}
				if (f.getName().endsWith("txt")) {
					String newName = f.getName().replace("txt", "java").replaceAll("Role", map.get("module"));
					System.out.println(outJavaPath + "/" + newName);
					FileOP.writeFile(outJavaPath + "/" + newName, s, "utf-8");
				} else if (f.getName().endsWith("jsp")) {
					System.out.println(outJspPath + "/" + f.getName());
					FileOP.writeFile(outJspPath + "/" + f.getName(), s, "utf-8");
				}
			}
		}
	}

	public static String getIdName(List<ColVO> cols) {
		for (ColVO vo : cols) {
			if (vo.isPk()) {
				String b = getFLE(vo.getName());
				return b;
			}
		}
		return "";
	}

	public static String getReqGet(List<ColVO> cols) {
		StringBuffer sb = new StringBuffer();
		for (ColVO vo : cols) {
			String b = getFLE(vo.getName());
			sb.append("			" + vo.getType() + " " + b + " = ServletRequestUtils.get" + getFB(vo.getType()) + "Parameter(req, \"" + b + "\");\r\n");
		}
		return sb.toString();
	}

	public static String getParas(List<ColVO> cols) {
		String s = "";
		for (ColVO vo : cols) {
			String b = getFLE(vo.getName());
			s += ", " + b;
		}
		return s.substring(2);
	}

	public static String getTypeParas(List<ColVO> cols) {
		String s = "";
		for (ColVO vo : cols) {
			String b = getFLE(vo.getName());
			s += ", " + vo.getType() + " " + b;
		}
		return s.substring(2);
	}

	public static String getDescs(List<ColVO> cols) {
		String s = "";
		for (ColVO vo : cols) {
			String b = getFLE(vo.getName());
			s += "	 * @param " + b + "\r\n";
			s += "	 *            " + vo.getDesc() + "\r\n";
		}
		return s.substring(0, s.length() - 2);
	}

	public static String getSetParas(List<ColVO> cols, String pre) {
		String s = "";
		for (ColVO vo : cols) {
			String a = getFBE(vo.getName());
			String b = getFLE(vo.getName());
			s += "		" + pre + ".set" + a + "(" + b + ");\r\n";
		}
		return s.substring(0, s.length() - 2);
	}

	public static String getFL(String s) {
		return s.substring(0, 1).toLowerCase() + s.substring(1);
	}

	public static String getFB(String s) {
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}

	public static String getFBE(String s) {
		String[] sa = s.split("_");
		String t = "";
		for (String ss : sa) {
			t += getFB(ss.toLowerCase());
		}
		return t;
	}

	public static String getFLE(String s) {
		return getFL(getFBE(s));
	}

}
